<?php
namespace app\admin\controller;

use app\common\model\Act as ActModel;
use app\common\model\Recharge as RechargeModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Member as MemberModel;
/**
 * 充值控制器
 * Class Recharge
 * @package app\admin\controller
 */
class Recharge extends Base
{
    /**
     * [getIndex  充值列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $model = new RechargeModel();
        $this->getPageAndSize($data);
        $map = [];
        $user = $this->user;
        if (is_array($data)) {
            if(!empty($data['key'])){
                if ($data['key'] == 1)
                    $map['am.username|am.tel'] = ['like', "%" . $data['con'] . "%"];
                elseif($data['key'] == 2)
                    $map['aa.title'] = ['like', "%" . $data['con'] . "%"];
                elseif($data['key'] == 3)
                    $map['ac.comname'] = ['like', "%" . $data['con'] . "%"];
                if(!empty($data['paytype'])){
                    if($data['paytype'] == 1){
                        $map['r.paytype'] = ['in','1,2'];
                    }elseif ($data['paytype'] == 2){
                        $map['r.paytype'] = 11;
                    }
                }

                if(!empty($data['states'])){
                    if($data['states'] == 2){
                        $map['r.states'] = 0;
                    }else{
                        $map['r.states'] = 1;
                    }
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['r.paytime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
            }else{
                if(!empty($data['paytype'])){
                    if($data['paytype'] == 1){
                        $map['paytype'] = ['in','1,2'];
                    }elseif ($data['paytype'] == 2){
                        $map['paytype'] = 11;
                    }
                }
                if(!empty($data['states'])){
                    if($data['states'] == 2){
                        $map['states'] = 0;
                    }else{
                        $map['states'] = 1;
                    }
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['paytime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
            }
            if(!empty($data['key'])){
                try{
                    $count = $model->getCount($map,$data['key']);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlist($map,$this->from, $this->size,$data['key']);
                    if ($data['key'] == 1){
                        foreach($lists as $k=>$v){
                            if(!empty($v['comid'])){
                                $cmodel = new CompanyModel();
                                $company = $cmodel->findData($v['comid']);
                                if(!empty($company)){
                                    $lists[$k]['comname'] = $company['comname'];
                                }
                            }
                            if(!empty($v['item_id'])){
                                $amodel = new ActModel();
                                $active = $amodel->findData($v['item_id']);
                                if(!empty($active)){
                                    $lists[$k]['actname'] = $active['title'];
                                }
                            }
                        }
                    }elseif($data['key'] == 2){
                        foreach($lists as $k=>$v){
                            if(!empty($v['comid'])){
                                $cmodel = new CompanyModel();
                                $company = $cmodel->findData($v['comid']);
                                if(!empty($company)){
                                    $lists[$k]['comname'] = $company['comname'];
                                }
                            }
                            if(!empty($v['memid'])){
                                $mmodel = new MemberModel();
                                $member = $mmodel->findData($v['memid']);
                                if(!empty($member)){
                                    $lists[$k]['tel'] = $member['tel'];
                                    $lists[$k]['username'] = $member['username'];
                                }
                            }
                        }
                    }elseif($data['key'] == 3){
                        foreach($lists as $k=>$v){
                            if(!empty($v['item_id'])){
                                $amodel = new ActModel();
                                $active = $amodel->findData($v['item_id']);
                                if(!empty($active)){
                                    $lists[$k]['actname'] = $active['title'];
                                }
                            }
                            if(!empty($v['memid'])){
                                $mmodel = new MemberModel();
                                $member = $mmodel->findData($v['memid']);
                                if(!empty($member)){
                                    $lists[$k]['tel'] = $member['tel'];
                                    $lists[$k]['username'] = $member['username'];
                                }
                            }
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
            }else{
                try{
                    $count = $model->getRechargeCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getRechargeByCondition($map,$this->from, $this->size);
                    foreach($lists as $k=>$v){
                        if(!empty($v['comid'])){
                            $cmodel = new CompanyModel();
                            $company = $cmodel->findData($v['comid']);
                            if(!empty($company)){
                                $lists[$k]['comname'] = $company['comname'];
                            }
                        }
                        if(!empty($v['item_id'])){
                            $amodel = new ActModel();
                            $active = $amodel->findData($v['item_id']);
                            if(!empty($active)){
                                $lists[$k]['actname'] = $active['title'];
                            }
                        }
                        if(!empty($v['memid'])){
                            $mmodel = new MemberModel();
                            $member = $mmodel->findData($v['memid']);
                            if(!empty($member)){
                                $lists[$k]['tel'] = $member['tel'];
                                $lists[$k]['username'] = $member['username'];
                            }
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
            }
        }

        writelog('查看充值明细列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}